C Program to Check if a Number Is Positive Or Negative

Check if a Number is Positive or Negative in C

Given an integer input, the objective is to write a code to Check if a Number is Positive or Negative in C Language.

For Instance

Input: Num = -5

Output: The number is Negative

Methods to Check Number Sign

Given an integer input, the objective is to check whether the given integer is Positive or Negative. In order to do so we have the following methods:

  • Method 1: Using Brute Force

    Algorithm 1

    For a user input num:

    If the num > 0: it is a positive number.

    If the num < 0: it is a negative number.

    Else the number has to be zero itself

    Same logic we have followed in the below C program.

    #include <stdio.h>
    int main() {
        int num;
        printf("Enter a number: ");
        scanf("%d", &num);
        if (num > 0) {
            printf("The number is Positive\n");
        } else if (num < 0) {
            printf("The number is Negative\n");
        } else {
            printf("The number is Zero\n");
        }
        return 0;
    }

    Output:

    Enter a number: -5
    The number is Negative
  • Method 2: Using Nested if-else Statements

    Algorithm 2

    1. Take input from the user.

    2. If the number is greater than zero, print "Positive".

    3. Else, check if the number is less than zero.

    4. If true, print "Negative".

    5. Otherwise, print "Zero".

    #include <stdio.h>
    int main() {
        int num;
        printf("Enter a number: ");
        scanf("%d", &num);
        if (num >= 0) {
            if (num == 0)
                printf("The number is Zero\n");
            else
                printf("The number is Positive\n");
        } else {
            printf("The number is Negative\n");
        }
        return 0;
    }

    Output:

    Enter a number: -8
    The number is Negative
  • Method 3: Using the ternary operator

    Algorithm 3

    1. Take input from the user.

    2. Use the ternary operator to check if the number is positive, negative, or zero.

    3. Print the result accordingly.

    #include <stdio.h>
    int main() {
        int num;
        printf("Enter a number: ");
        scanf("%d", &num);
        (num > 0) ? printf("The number is Positive\n") : 
        (num < 0) ? printf("The number is Negative\n") : 
                    printf("The number is Zero\n");
        return 0;
    }

    Output:

    Enter a number: 0
    The number is Zero
Easy aceess next quctions
Getting Started

Positive or Negative number: C C++ Java Python

Even or Odd number: C C++ Java Python

Sum of First N Natural numbers: C C++ Java Python

Sum of N natural numbers: C C++ Java Python

Sum of numbers in a given range: C C++ Java Python

Greatest of two numbers: C C++ Java Python

Greatest of the Three numbers: C C++ Java Python

Leap year or not: C C++ Java Python

Prime number: C C++ Java Python

Prime number within a given range: C C++ Java Python

Sum of digits of a number: C C++ Java Python

Reverse of a number: C C++ Java Python

Palindrome number: C C++ Java Python

Armstrong number: C C++ Java Python

Armstrong number in a given range: C C++ Java Python

Harshad number: C C++ Java Python

Abundant number: C C++ Java Python

Friendly pair: C C++ Java Python